package com.xqq.lottery;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xqq.lottery.adapter.MessageAdapter;
import com.xqq.lottery.bean.ErrorInfo;
import com.xqq.lottery.bean.Message;
import com.xqq.lottery.bean.Option;
import com.xqq.lottery.bean.User;
import com.xqq.lottery.engine.UserEngineImpl;
import com.xqq.lottery.net.NoticeService;
import com.xqq.lottery.view.MessageInputToolBox;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;

public class MessageActivity extends FragmentActivity {
	
	private static final String TAG = "MainActivity";
	
    private MessageInputToolBox box;	
    private ListView msgListView;
    private MessageAdapter messageAdapter;
    private List<Message> messageList =new ArrayList<Message>();
	
	static{
		System.loadLibrary("lottery");
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.layout_main);
		super.onCreate(savedInstanceState);
		//开启push服务
		initView();
		new Thread(){
			public void run() {
				User user =new User();
				user.setUsername("xqq");
				user.setPassword("123");
				UserEngineImpl uei =new UserEngineImpl();
				ErrorInfo msg = uei.login(user);
				if(msg != null){
					Log.i(TAG, msg.getCode()+","+msg.getMsg());
				}
			};
		}.start();
	}
	
	public void startPushService(){
		Intent intent =new Intent();
		intent.setClass(this, NoticeService.class);
		startService(intent);
	}
	
	public void initView(){
		
		msgListView = (ListView) findViewById(R.id.messageListview);
		messageAdapter =new MessageAdapter(this, messageList);
		msgListView.setAdapter(messageAdapter);
		
		box = (MessageInputToolBox) findViewById(R.id.message_input_box);
		ArrayList<String> faceNameList = new ArrayList<String>();
		for(int x = 1; x <= 10; x++){
			faceNameList.add("big"+x);
		}
		for(int x = 1; x <= 10; x++){
			faceNameList.add("big"+x);
		}
		
		ArrayList<String> faceNameList1 = new ArrayList<String>();
		for(int x = 1; x <= 7; x++){
			faceNameList1.add("cig"+x);
		}
		
		
		ArrayList<String> faceNameList2 = new ArrayList<String>();
		for(int x = 1; x <= 24; x++){
			faceNameList2.add("dig"+x);
		}
		
		Map<Integer, ArrayList<String>> faceData = new HashMap<Integer, ArrayList<String>>();
		faceData.put(R.drawable.em_cate_magic, faceNameList2);
		faceData.put(R.drawable.em_cate_rib, faceNameList1);
		faceData.put(R.drawable.em_cate_duck, faceNameList);
		box.setFaceData(faceData);
		
		List<Option> functionData = new ArrayList<Option>();
		for(int x = 0; x < 5; x++){
			Option takePhotoOption = new Option(this, "Take", R.drawable.take_photo);
			Option galleryOption = new Option(this, "Gallery", R.drawable.gallery);
			functionData.add(galleryOption);
			functionData.add(takePhotoOption);
		}
		Log.i(TAG, "functionData.size="+functionData.size());
		box.setFunctionData(functionData);		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			if(box.isBottomVisibility()){
				box.hideFunctionLayout();
				box.hideFaceLayout();
				return true;
			}
			break;
		case KeyEvent.KEYCODE_HOME:
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
}
